﻿using EquipViewAppNet;
using Newtonsoft.Json.Linq;
using SecsEquip;
using SecsEquip300;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;

namespace EquipSecs300
{
    public class GEM_PPFormat_All : GEM_PPFormat
    {
        public GEM_PPFormat_All(CSecsEquip300 m_pSecs, SecsEquipHelper secsEquipHelper) : base(m_pSecs, secsEquipHelper)
        {

        }
        // Format上传
        public override void OnPPFormatHandleEvent(object sender, PPFormatEventArgs e)
        {
            string ppid = "";
            if (e.nHandleType == PPFormatHandle.LoadPpid)
            {
                ppid = e.ppid;
                recipeItem_DictTwoSend = FuGetRecipeItemHandler?.Invoke(ppid, out recipeItemType_DictTwoSend);
                recipeItem_DictOneSend = ConvertRecipeItem_DictTwoToOne(recipeItem_DictTwoSend);
                recipeItemType_DictOneSend = ConvertRecipeItem_DictTwoToOne(recipeItemType_DictTwoSend);
                if (!IsPPFormatAdded)
                {
                    PPFormatAdd(recipeItem_DictOneSend, recipeItemType_DictOneSend);
                }
            }
            // 逐一把数据返回        
            else if (e.nHandleType == PPFormatHandle.GetPpidValue)
            {
                List<string> listValue = new List<string>();
                if (recipeItem_DictOneSend != null)
                {
                    try
                    {
                        switch (e.pCmdCode)
                        {

                            default:
                                {
                                    foreach (var item in recipeItem_DictOneSend)
                                    {
                                        if (e.pCmdCode == item.Key)
                                        {
                                            listValue.Add(item.Value);
                                            LogMsg($"Send Format Recipe -> Name: {e.pCmdCode}, value: {listValue[0]}");
                                            break;
                                        }
                                    }
                                }
                                break;
                        }
                    }
                    catch (Exception ex)
                    {
                        LogMsg(ex.Message, MsgSource_KE.EQ);
                    }

                }

                e.listParamValue = listValue;
            }
        }

        // Format 下载
        public override void OnPPFormatChangeHandleEvent(object sender, PPFormatEventArgs e)
        {
            // 新建或者打开已有的一个recipe        
            if (e.nHandleType == PPFormatHandle.SetPpidVlaueBegin)
            {
                ppid = e.ppid;
                recipeItem_DictTwoCur = FuGetRecipeItemHandler?.Invoke(ppid, out recipeItemType_DictTwoCur);
                recipeItem_DictOneCur = ConvertRecipeItem_DictTwoToOne(recipeItem_DictTwoCur);
                listRecipeItem.Clear();
                recipeItem_DictOneRec.Clear();
                if (recipeItem_DictTwoCur!=null)
                {
                    recipeItem_DictTwoRec = UtilitiesFile.DeepCopy(recipeItem_DictTwoCur);
                }
                if (!IsPPFormatAdded)
                {
                    PPFormatAdd(recipeItem_DictOneSend, recipeItemType_DictOneSend);
                }
            }

            // 向打开的recipe写入数据          
            else if (e.nHandleType == PPFormatHandle.SetPpidVlaue)
            {
                List<string> listValue = e.listParamValue;
                int n = 0;
                if (recipeItem_DictTwoSend != null)
                {
                    try
                    {
                        switch (e.pCmdCode)
                        {
                            default:
                                {
                                    if (!recipeItem_DictOneRec.ContainsKey(e.pCmdCode))
                                    {
                                        recipeItem_DictOneRec.Add(e.pCmdCode, listValue[0]);
                                    }
                                    else
                                    {
                                        LogMsg($"ERR：接收Host RecipeItem键值对{e.pCmdCode}重复");
                                    }
                                    recipeItem_DictTwoRec = UpdateRecipeDicTwo(recipeItem_DictTwoRec, e.pCmdCode, listValue[0]);
                                    LogMsg($"Receive Format Recipe -> Name: {e.pCmdCode}, value: {listValue[0]}");
                                }
                                break;
                        }
                    }
                    catch (Exception ex)
                    {
                        LogMsg(ex.Message, MsgSource_KE.EQ);
                    }

                }

            }

            // 保存recipe      
            else if (e.nHandleType == PPFormatHandle.SetPpidVlaueFinsh)
            {
                FuSetRecipeItemHandler?.Invoke(ppid, recipeItem_DictTwoRec);
            }
        }


        /// <summary>
        /// 根据EAP给的PPID和PP Body，在本地对应的工单中更新值
        /// </summary>
        /// <param name="recipeItem_DictTwoModel"></param>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        private Dictionary<string, Dictionary<string, string>> UpdateRecipeDicTwo(Dictionary<string, Dictionary<string, string>> recipeItem_DictTwoModel, string name, string value)
        {
            if (recipeItem_DictTwoModel == null) { return null; }
            bool isContain = false;
            Dictionary<string, Dictionary<string, string>> dictRecipeItemTemp = UtilitiesFile.DeepCopy(recipeItem_DictTwoModel);

            foreach (var item in recipeItem_DictTwoModel)
            {
                foreach (var itemSub in item.Value)
                {
                    if (itemSub.Key == name)
                    {
                        isContain = true;
                        try
                        {
                            dictRecipeItemTemp[item.Key][itemSub.Key] = value;
                        }
                        catch (Exception ex)
                        {
                            LogMsg(ex.Message, MsgSource_KE.EQ);
                        }

                    }
                }
            }
            if (!isContain)
            {
                LogMsg($"ERR： EAP发送Recipe Body -> name: {name}, vale: {value}, 不存在本地工单中", MsgSource_KE.EQ);
            }
            return dictRecipeItemTemp;

        }

        public Dictionary<string, string> ConvertRecipeItem_DictTwoToOne(Dictionary<string, Dictionary<string, string>> sourRecipeItem)
        {
            if (sourRecipeItem == null || sourRecipeItem.Count == 0)
            {
                return null;
            }
            Dictionary<string, string> retRecipeItem = new Dictionary<string, string>();
            foreach (var item in sourRecipeItem)
            {
                foreach (var itemSub in item.Value)
                {
                    retRecipeItem.Add(itemSub.Key, itemSub.Value);
                }
            }
            return retRecipeItem;
        }
        private Dictionary<string, Dictionary<string, string>> ConvertRecipeItem_DictOneToTwo(Dictionary<string, string> sourRecipeItem, Dictionary<string, Dictionary<string, string>> RecipeItem_TwoModel)
        {
            Dictionary<string, Dictionary<string, string>> retRecipeItem = new Dictionary<string, Dictionary<string, string>>(RecipeItem_TwoModel);
            foreach (var item in RecipeItem_TwoModel)
            {
                foreach (var itemSub in item.Value)
                {
                    foreach (var PPitem in sourRecipeItem)
                    {
                        if (itemSub.Key.Equals(PPitem))
                        {
                            try
                            {
                                retRecipeItem[item.Key][itemSub.Key] = PPitem.Value;
                            }
                            catch (Exception ex)
                            {
                                LogMsg(ex.Message);
                            }

                        }
                    }
                }
            }
            return retRecipeItem;
        }


        protected void ConvertDictTwoToList()
        {
            List<string> listItem = new List<string>();
            listRecipeItem.Clear();
            foreach (var item in recipeItem_DictTwoSend)
            {
                foreach (var itemSub in item.Value)
                {
                    listItem.Add(itemSub.Value);
                    listRecipeItem.Add(itemSub.Value);
                }
            }
        }

    }
}
